package com.example.service;

import com.example.common.Result;
import com.example.common.enums.FeedbackStatus;
import com.example.entity.Feedback;
import com.example.mapper.FeedbackMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author HY
 * @date 2025/2/9
 */
@Service
public class FeedbackService {
    @Autowired
    private FeedbackMapper feedbackMapper;

    @Transactional(rollbackFor = Exception.class)
    public Result add(Feedback feedback) {
        feedback.setStatus(FeedbackStatus.UNRESOLVED.getValue());
        feedbackMapper.insert(feedback);
        if (feedback.getImgs() != null && feedback.getImgs().size() > 0) {
            feedbackMapper.insertFeedbackImgs(feedback.getId(), feedback.getImgs());
        }
        return Result.success();
    }

    public Result audit(Feedback feedback) {
        Feedback feedback1 = new Feedback();
        feedback1.setId(feedback.getId());
        feedback1.setStatus(FeedbackStatus.RESOLVED.getValue());
        feedback1.setBackContent(feedback.getBackContent());
        feedbackMapper.updateById(feedback1);
        return Result.success();
    }

    public PageInfo<Feedback> selectPage(Feedback feedback, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Feedback> list = feedbackMapper.selectList(feedback);
        return PageInfo.of(list);
    }

    public Feedback selectById(Integer id) {
        return feedbackMapper.selectById(id);
    }
}
